require("Planet")
require("Collector")
require("BouncyBlock")
require 'Asteroid' 
require 'SpriteHelper'
require 'Platform'
require 'PlatformTrans'
require 'GravitySensor'

M = {}

local function split(pString, pPattern)
    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end
    return Table
end

local function getSheetName(filename)
    local tPath = split(filename, "\\")
    return string.sub(tPath[#tPath],0,string.len(tPath[#tPath])-4)
end

local newLevel = function(levelXml,imageSheet)
    local backgroundGroup = display.newImageGroup( imageSheet )
    local bugGroup = display.newGroup()
    local planetGroup = display.newImageGroup( imageSheet )
    
    local xml = require("xml").newParser()
    local level = xml:loadFile(levelXml)
    local layer = level.child[1]
    for i=1,#layer.child do
        local objectType = layer.child[i].properties["Name"]
        if(objectType == "PLANETAS")then
            local planetas = layer.child[i].child[1]
            for j=1,#planetas.child do
                local planeta = planetas.child[j]
                local x = planeta.child[1].child[1].value
                local y = planeta.child[1].child[2].value
                local scale = planeta.child[4].child[1].value
                local filename = planeta.child[8].value
                local sheetName = getSheetName(filename)
                local radio = planeta.child[10].child[1].value
                local planet1 = Planet:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,radio * scale, 150);
                planetGroup:insert(planet1.body)
                backgroundGroup:insert( planet1.atmosferaCircle )
                --add bugs
                local bugs = 1
                if(planeta.child[2].child[1] ~= nil) then
                    bugs = planeta.child[2].child[1].child[1].value
                end
                for i=1,bugs do
                    local bug = Bug:new(imageSheet,SpriteHelper.sheets.kiwi,planet1)
                    bugGroup:insert(bug.body)
                end
            end
        end
        if(objectType == "COLLECTORS")then
            local collectors = layer.child[i].child[1]
            for k=1,#collectors.child do
                local collector = collectors.child[k]
                local x = collector.child[1].child[1].value
                local y = collector.child[1].child[2].value
                local filename = collector.child[8].value
                local sheetName = getSheetName(filename)
                local collector1 = Collector:new(imageSheet,SpriteHelper.sheets[sheetName],x,y);
                backgroundGroup:insert( collector1.body )
                
            end
        end
        
        if(objectType == "BACKGROUND")then
            local backgrounds = layer.child[i].child[1]
            for k=1,#backgrounds.child do
                local background = backgrounds.child[k]
                local x = background.child[1].child[1].value
                local y = background.child[1].child[2].value
                local rotation= math.deg(background.child[3].value)
                local scale = background.child[4].child[1].value
                local sizex = background.child[10].child[1].value * 2
                local sizey = background.child[10].child[2].value * 2
                local filename = background.child[8].value
                local sheetName = getSheetName(filename)
                local back1 = display.newImageRect(imageSheet,SpriteHelper.sheets[sheetName],sizex*scale,sizey*scale);
                back1.rotation = rotation
                back1.x = x
                back1.y = y
                
                backgroundGroup:insert( back1 )
                
            end
        end 
        if(objectType == "BLOCKS" or objectType == "ASTEROID" or objectType == "PLATFORM" or objectType == "GSENSOR" or objectType == "PLATFORMTRANS")then
            local planetas = layer.child[i].child[1]
            for j=1,#planetas.child do
                local planeta = planetas.child[j]
                local x = planeta.child[1].child[1].value
                local y = planeta.child[1].child[2].value
                local rotation= math.deg(planeta.child[3].value)
                local scalex = planeta.child[4].child[1].value
                local scaley = planeta.child[4].child[2].value
                local filename = planeta.child[8].value
                local sheetName = getSheetName(filename)
                local sizex = planeta.child[10].child[1].value * 2
                local sizey = planeta.child[10].child[2].value * 2
                local block1 = nil
                if(objectType == "BLOCKS") then 
                    block1 = BouncyBlock:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,sizex * scalex, sizey * scaley, rotation);
                elseif(objectType == "ASTEROID")then
                    block1 = Asteroid:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,sizex * scalex, sizey * scaley, rotation);
                elseif(objectType == "PLATFORM")then
                    block1 = Platform:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,sizex * scalex, sizey * scaley, rotation);
                elseif(objectType == "PLATFORMTRANS")then
                    block1 = PlatformTrans:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,sizex * scalex, sizey * scaley, rotation);
                elseif(objectType == "GSENSOR")then
                    block1 = GravitySensor:new(imageSheet,SpriteHelper.sheets[sheetName],x,y,sizex * scalex, sizey * scaley, rotation);
                end      
                backgroundGroup:insert( block1.body )
            end
        end
    end
    
    return planetGroup,backgroundGroup, bugGroup
end

M.newLevel = newLevel

return M